package com.xiaozhen.tcp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaozhen.common.core.domain.driver.ParseTcpAttribute;
import com.xiaozhen.common.core.domain.driver.ParseGroup;
import com.xiaozhen.common.core.domain.driver.ParseTcpDriver;
import com.xiaozhen.tcp.config.TcpConfig;
import com.xiaozhen.tcp.dao.ParseAttributeDao;
import com.xiaozhen.tcp.dao.TcpDriverDao;
import com.xiaozhen.tcp.service.DriverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/9/28 16:02
 * @description:
 */
@Service
@Slf4j
public class DriverServiceImpl implements DriverService {

    @Autowired
    private TcpDriverDao tcpDriverDao;
    @Autowired
    private ParseAttributeDao parseAttributeDao;
    @Autowired
    private TcpConfig tcpConfig;

    @Override
    public ParseTcpDriver selectTcpDriver() {
        log.info("端口：{}", tcpConfig.getPort());
        QueryWrapper<ParseTcpDriver> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("port", tcpConfig.getPort());
        return tcpDriverDao.selectOne(queryWrapper);
    }

    @Override
    public Map<Integer, ParseGroup> selectGroupByDriverId(Long driverId) {
        return null;
    }

    @Override
    public List<ParseTcpAttribute> selectAttributeList(Long driverId) {
        QueryWrapper<ParseTcpAttribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id", driverId);
        List<ParseTcpAttribute> attributes = parseAttributeDao.selectList(queryWrapper);
        return getChildPerms(attributes, 0);
    }


    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list     分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public List<ParseTcpAttribute> getChildPerms(List<ParseTcpAttribute> list, int parentId) {
        List<ParseTcpAttribute> returnList = new ArrayList<>();
        for (Iterator<ParseTcpAttribute> iterator = list.iterator(); iterator.hasNext(); ) {
            ParseTcpAttribute t = iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == parentId) {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<ParseTcpAttribute> list, ParseTcpAttribute t) {
        // 得到子节点列表
        List<ParseTcpAttribute> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ParseTcpAttribute tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<ParseTcpAttribute> getChildList(List<ParseTcpAttribute> list, ParseTcpAttribute t) {
        List<ParseTcpAttribute> tlist = new ArrayList<>();
        Iterator<ParseTcpAttribute> it = list.iterator();
        while (it.hasNext()) {
            ParseTcpAttribute n = it.next();
            if (n.getParentId().longValue() == t.getAttributeId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ParseTcpAttribute> list, ParseTcpAttribute t) {
        return getChildList(list, t).size() > 0;
    }

}
